import React from 'react';
import { Result, Button } from 'antd';
import { 
  ExclamationCircleOutlined, 
  CloseCircleOutlined, 
  CheckCircleOutlined,
  InfoCircleOutlined 
} from '@ant-design/icons';

interface ErrorBoundaryProps {
  status?: 'error' | 'warning' | 'success' | 'info' | '404' | '403' | '500';
  title?: string;
  subTitle?: string;
  extra?: React.ReactNode;
  onRetry?: () => void;
  onGoHome?: () => void;
}

const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({
  status = 'error',
  title,
  subTitle,
  extra,
  onRetry,
  onGoHome
}) => {
  const getIcon = () => {
    switch (status) {
      case 'warning':
        return <ExclamationCircleOutlined />;
      case 'success':
        return <CheckCircleOutlined />;
      case 'info':
        return <InfoCircleOutlined />;
      default:
        return <CloseCircleOutlined />;
    }
  };

  const getDefaultTitle = () => {
    switch (status) {
      case '404':
        return '页面不存在';
      case '403':
        return '访问被拒绝';
      case '500':
        return '服务器错误';
      case 'warning':
        return '警告';
      case 'success':
        return '成功';
      case 'info':
        return '提示';
      default:
        return '出现错误';
    }
  };

  const getDefaultSubTitle = () => {
    switch (status) {
      case '404':
        return '抱歉，您访问的页面不存在。';
      case '403':
        return '抱歉，您没有权限访问此页面。';
      case '500':
        return '抱歉，服务器出现错误。';
      default:
        return '请稍后重试或联系管理员。';
    }
  };

  const defaultExtra = (
    <div style={{ display: 'flex', gap: 8, justifyContent: 'center' }}>
      {onRetry && (
        <Button type="primary" onClick={onRetry}>
          重试
        </Button>
      )}
      {onGoHome && (
        <Button onClick={onGoHome}>
          返回首页
        </Button>
      )}
    </div>
  );

  return (
    <div style={{
      display: 'flex',
      justifyContent: 'center',
      alignItems: 'center',
      minHeight: '400px',
      padding: '20px'
    }}>
      <Result
        status={status}
        icon={getIcon()}
        title={title || getDefaultTitle()}
        subTitle={subTitle || getDefaultSubTitle()}
        extra={extra || defaultExtra}
      />
    </div>
  );
};

export default ErrorBoundary;